home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / think / AmigaGnuChess.lha / chess / src.lha / src / gnuchess.h < prev    next >
C/C++ Source or Header  |  1992-09-06  |  18KB  |  605 lines

  1. /*
  2.  * gnuchess.h - Header file for GNU CHESS
  3.  *
  4.  * Copyright (c) 1988,1989,1990 John Stanback
  5.  * Copyright (c) 1992 Free Software Foundation
  6.  *
  7.  * This file is part of GNU CHESS.
  8.  *
  9.  * GNU Chess is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2, or (at your option)
  12.  * any later version.
  13.  *
  14.  * GNU Chess is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with GNU Chess; see the file COPYING.  If not, write to
  21.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23.  
  24. #if !defined(__STDC__) || !defined(MSDOS)
  25. #define const
  26. #endif
  27.  
  28. #ifndef __GNUC__
  29. #define inline
  30. #endif
  31.  
  32. #include <stdio.h>
  33.  
  34. #define SEEK_SET 0
  35. #define SEEK_END 2
  36. #ifdef DEBUG
  37. void
  38. ShowDBLine (char *, short int, short int,
  39.         short int, short int, short int,
  40.         short unsigned int *);
  41.      extern FILE *debugfd;
  42.      extern short int debuglevel;
  43.  
  44. #endif /* DEBUG */
  45.  
  46. #include <ctype.h>
  47.  
  48. #ifdef MSDOS
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include <time.h>
  52. #define RWA_ACC "r+b"
  53. #define WA_ACC "w+b"
  54. #else
  55. #define RWA_ACC "r+"
  56. #define WA_ACC "w+"
  57. #ifndef AMIGA
  58. #include <sys/param.h>
  59. #endif
  60. #include <sys/types.h>
  61. #ifndef AMIGA
  62. #include <sys/times.h>
  63. #endif
  64. #endif /* MSDOS */
  65. #ifdef NONDSP
  66. #define printz printf
  67. #define scanz scanf
  68. #else
  69. #ifdef AMIGA
  70. #include "amiga/amiterm.h"
  71. #define scanz ami_scanw
  72. extern char *libdir;
  73. #else
  74. #include <curses.h>
  75. #define scanz fflush(stdout), scanw
  76. #endif
  77. #define printz printw
  78. #endif
  79.  
  80. #ifndef AMIGA
  81. #if defined(__STDC__) || defined(MSDOS)
  82. /* <stdio.h> */
  83.      extern int fclose (FILE *);
  84. #ifndef __ultrix /* work around bug in c89 compiler --t.mann */
  85.      extern int fscanf (FILE *, const char *, ...);
  86.      extern int fprintf (FILE *, const char *, ...);
  87. #endif /*__ultrix*/
  88.      extern int fflush (FILE *);
  89.  
  90. /* <stdlib.h> */
  91.      extern int abs (int);
  92.      extern int atoi (const char *);
  93.  
  94. /* <time.h> */
  95.      extern long int time (long int *);
  96.  
  97. /* <string.h> */
  98.      extern void *memset (void *, int, size_t);
  99. #endif
  100. #endif /* AMIGA */
  101.  
  102. /* Piece values */
  103. #define valueP 100
  104. #define valueN 350
  105. #define valueB 355
  106. #define valueR 550
  107. #define valueQ 1100
  108. #define valueK 1200
  109. /* masks into upper 8 bits of ataks array */
  110. #define ctlP 0x4000
  111. #define ctlN 0x2800
  112. #define ctlB 0x1800
  113. #define ctlR 0x0400
  114. #define ctlQ 0x0200
  115. #define ctlK 0x0100
  116. #define ctlBQ 0x1200
  117. #define ctlBN 0x0800
  118. #define ctlRQ 0x0600
  119. #define ctlNN 0x2000
  120. /* attack functions */
  121. #define Patak(c, u) (atak[c][u] > ctlP)
  122. #define Anyatak(c, u) (atak[c][u] > 0)
  123. /* distance function */
  124. #define taxicab(a,b) taxidata[a][b]
  125. /* hashtable flags */
  126. #define truescore 0x0001
  127. #define lowerbound 0x0002
  128. #define upperbound 0x0004
  129. #define kingcastle 0x0008
  130. #define queencastle 0x0010
  131. /* king positions */
  132. #define wking PieceList[white][0]
  133. #define bking PieceList[black][0]
  134. #define EnemyKing PieceList[c2][0]
  135. /* constants */
  136. /* castle moves */
  137. #define BLACKCASTLE    0x3C3E
  138. #define WHITECASTLE    0x0406
  139. #define LONGBLACKCASTLE    0x3C3A
  140. #define LONGWHITECASTLE    0x0402
  141. /* truth values */
  142. #define false 0
  143. #define true 1
  144. /* colors */
  145. #define white 0
  146. #define black 1
  147. #define neutral 2
  148. /* piece code defines */
  149. #define no_piece 0
  150. #define pawn 1
  151. #define knight 2
  152. #define bishop 3
  153. #define rook 4
  154. #define queen 5
  155. #define king 6
  156. #define bpawn 7
  157. /* node flags */
  158. #define pmask 0x0007
  159. #define promote 0x0008
  160. #define cstlmask 0x0010
  161. #define epmask 0x0020
  162. #define exact 0x0040
  163. #define pwnthrt 0x0080
  164. #define check 0x0100
  165. #define capture 0x0200
  166. #define draw 0x0400
  167. /* move symbols */
  168. #define pxx (CP[2])
  169. #define qxx (CP[1])
  170. #define rxx (CP[4])
  171. #define cxx (CP[3])
  172. /* for everything that can't use the above */
  173. #define Qxx " pnbrqk"
  174. #define Pxx " PNBRQK"
  175. #define Cxx "abcdefgh"
  176. #define Rxx "12345678"
  177. /***************************************************************************/
  178. /***************** Table limits ********************************************/
  179. /*
  180.  * ttblsz must be a power of 2. Setting ttblsz 0 removes the transposition
  181.  * tables.
  182.  */
  183. #if defined(MSDOS) || defined(AMIGA)
  184. #define vttblsz 0 /*(1 << 11)*/
  185. #else
  186. #define vttblsz (1 << 17)
  187. #define huge
  188. #endif /* MSODS */
  189.  
  190. #define ttblsz vttblsz
  191. #define TREE 1500        /* max number of tree entries */
  192. #define MAXDEPTH 35        /* max depth a search can be carried */
  193. #define MINDEPTH 3        /* min search depth =1 (no hint), >1 hint */
  194. #define MAXMOVES 400        /* max number of half moves in a game */
  195. #ifdef AMIGA
  196. #define BOOKSIZE 10000        /* Number of unique position/move combinations allowed */
  197. #else
  198. #define BOOKSIZE 30000        /* Number of unique position/move combinations allowed */
  199. #endif
  200. #define CPSIZE 226        /* size of lang file max */
  201. /***************** tuning paramaters **********************************************/
  202. #define CHKDEPTH 1        /* always look forward CHKDEPTH half-moves if in check */
  203. #define DEPTHBEYOND 11        /* Max to go beyond Sdepth */
  204. #define HASHDEPTH 4        /* depth above which to use HashFile */
  205. #define HASHMOVELIMIT 40    /* Use HashFile only for this many moves */
  206. #define PTVALUE 2000            /* material value below which pawn threats at 5 & 3 are used */
  207. #define ZDEPTH 3        /* depth beyond which to check ZDELTA for extra time */
  208. #define ZDELTA 10        /* score delta per ply to cause extra time to be given */
  209. #define ZNODES 1000        /* check the time every ZNODES positions */
  210. #define MAXTCCOUNT 4        /* max number of time clicks per search */
  211. #define MINSEARCHPCT 10        /* must have looked at MINSEARCHPCT moves on a ply on a timeout */
  212. #define SCORETIME -50        /* score below which to add search time */
  213. #define SCORESPLIM 8        /* Score space doesn't apply after this stage */
  214. #define HISTORYLIM 155        /* Max value of history killer */
  215. #define EWNDW 10        /* Eval window to force position scoring at depth greater than Sdepth + 2 */
  216. #define WAWNDW 90        /* alpha window when computer white*/
  217. #define WBWNDW 90        /* beta window when computer white*/
  218. #define BAWNDW 90        /* alpha window when computer black*/
  219. #define BBWNDW 90        /* beta window when computer black*/
  220. #define BXWNDW 90        /* window to force position scoring at lower */
  221. #define WXWNDW 90        /* window to force position scoring at lower */
  222. #define DITHER 2        /* 1+max amount random can alter a pos value */
  223. #define BBONUS 2        /* points per stage value of B increases */
  224. #define RBONUS 6        /* points per stage value of R increases */
  225. #define KINGPOSLIMIT ( -1)    /* King positional scoring limit */
  226. #define KINGSAFETY  16
  227. #define MAXrehash (6)
  228.  
  229. #if defined AG0
  230. #define WHITEAG0
  231. #define BLACKAG0
  232.  
  233. #elif defined AG1
  234. #define WHITEAG1
  235. #define BLACKAG1
  236.  
  237. #elif defined AG2
  238. #define WHITEAG2
  239. #define BLACKAG2
  240.  
  241. #elif defined AG3
  242. #define WHITEAG3
  243. #define BLACKAG3
  244.  
  245. #elif defined AGB
  246. #define WHITEAG2
  247. #define BLACKAG2
  248.  
  249. #elif defined AG4
  250. #define WHITEAG4
  251. #define BLACKAG4
  252. #endif
  253. /************************* parameters for Opening Book *********************************/
  254. #define BOOKBITS 10        /* # bits for hashtable to book moves */
  255. #define BOOKFAIL 20000        /* if no book move found for BOOKFAIL turns stop using book */
  256. #define BOOKRAND 1000        /* used to select an opening move from a list */
  257. #define BOOKENDPCT 500        /* 50 % chance a BOOKEND will stop the book */
  258. #define DONTUSE -32768        /* flag move as don't use */
  259. /*************************** Book access defines ****************************************/
  260. #define BKTBLSIZE (2<<BOOKBITS)
  261. #define BOOKMASK (BKTBLSIZE - 1)
  262. #define SIDEMASK 0x1
  263. #define BOOKEND 0x2        /* means this is the last move of an opening */
  264. #define BADMOVE 0x8000        /* means this is a bad move in this position */
  265. /****************************************************************************************/
  266.      struct hashval
  267.      {
  268.        unsigned long key, bd;
  269.      };
  270.      struct hashentry
  271.      {
  272.        unsigned long hashbd;
  273.        unsigned short mv;
  274.        unsigned char flags, depth;    /* char saves some space */
  275.        short score;
  276. #ifdef HASHTEST
  277.        unsigned char bd[32];
  278. #endif /* HASHTEST */
  279.  
  280.      };
  281.  
  282. #ifdef HASHFILE
  283.  
  284. /*
  285.  * persistent transposition table. The size must be a power of 2. If you
  286.  * change the size, be sure to run gnuchess -c before anything else.
  287.  */
  288. #define frehash 6
  289. #ifdef MSDOS
  290. #define Deffilesz (1 << 13) -1
  291. #else
  292. #define Deffilesz (1 << 17) -1
  293. #endif /* MSDOS */
  294.      struct fileentry
  295.      {
  296.        unsigned char bd[32];
  297.        unsigned char f, t, flags, depth, sh, sl;
  298.      };
  299.  
  300. #endif /* HASHFILE */
  301.  
  302.  
  303.      struct leaf
  304.      {
  305.        short f, t, score, reply, width;
  306.        unsigned short flags;
  307.      };
  308.      struct GameRec
  309.      {
  310.        unsigned short gmove;    /* this move */
  311.        short score;        /* score after this move */
  312.        short depth;        /* search depth this move */
  313.        short piece;        /* piece captured */
  314.        short color;        /* color */
  315.        short flags;        /* move flags capture, promote, castle */
  316.        short Game50;        /* flag for repetition */
  317.        int time;        /* search time this move */
  318.        long nodes;        /* nodes searched for this move */
  319.        long hashkey, hashbd;    /* board key before this move */
  320. #ifdef DEBUG40
  321.        int d1;
  322.        int d2;
  323.        int d3;
  324.        int d4;
  325.        int d5;
  326.        int d6;
  327.        int d7;
  328. #endif
  329.      };
  330.      struct TimeControlRec
  331.      {
  332.        short moves[2];
  333.        long clock[2];
  334.      };
  335.  
  336.      struct flags
  337.      {
  338.        short mate;        /* the game is over */
  339.        short post;        /* show principle variation */
  340.        short quit;        /* quit/exit */
  341.        short regularstart;    /* did the game start from standard
  342.                  * initial board ? */
  343.        short reverse;        /* reverse board display */
  344.        short bothsides;        /* computer plays both sides */
  345.        short hash;        /* enable/disable transposition table */
  346.        short force;        /* enter moves */
  347.        short easy;        /* disable thinking on opponents time */
  348.        short beep;        /* enable/disable beep */
  349.        short timeout;        /* time to make a move */
  350.        short musttimeout;    /* time to make a move */
  351.        short rcptr;        /* enable/disable recapture heuristics */
  352.        short rv;        /* reverse video */
  353.        short stars;        /* add stars to uxdsp screen */
  354.        short coords;        /* add coords to visual screen */
  355.        short shade;
  356.        short material;        /* draw on lack of material */
  357.        short illegal;        /* illegal position */
  358.        short onemove;        /* timing is onemove */
  359.      };
  360.  
  361. #ifdef DEBUG
  362.      extern FILE *debugfile;
  363.  
  364. #endif /* DEBUG */
  365. #ifdef HISTORY
  366.      extern unsigned char __far history[32768];
  367. #endif
  368.      extern char *ColorStr[2];
  369.      extern unsigned short int MV[MAXDEPTH];
  370.      extern int MSCORE;
  371.      extern int mycnt1, mycnt2;
  372.      extern short int ahead;
  373.      extern short int chesstool;
  374.      extern struct leaf Tree[], *root, rootnode;
  375.      extern char savefile[], listfile[];
  376.      extern short TrPnt[];
  377.      extern short board[], color[];
  378.      extern short PieceList[2][64], PawnCnt[2][8];
  379.      extern short castld[], Mvboard[];
  380.      extern short svalue[];
  381.      extern struct flags flag;
  382.      extern short opponent, computer, INCscore;
  383.      extern short WAwindow, BAwindow, WBwindow, BBwindow;
  384.      extern short dither, player;
  385.      extern short xwndw, epsquare, contempt;
  386.      extern long ResponseTime, ExtraTime, MaxResponseTime, et, et0, time0, ft;
  387.      extern long reminus, replus;
  388.      extern long NodeCnt, ETnodes, EvalNodes, HashAdd, HashCnt, HashCol, THashCol,
  389.       FHashCnt, FHashAdd;
  390.      extern short HashDepth, HashMoveLimit;
  391.      extern struct GameRec __far GameList[];
  392.      extern short GameCnt, Game50;
  393.      extern short Sdepth, MaxSearchDepth;
  394.      extern int Book;
  395.      extern struct TimeControlRec TimeControl;
  396.      extern short TCflag, TCmoves, TCminutes, TCseconds, OperatorTime;
  397.      extern short XCmore, XCmoves[], XCminutes[], XCseconds[], XC;
  398.      extern const short otherside[];
  399.      extern const short Stboard[];
  400.      extern const short Stcolor[];
  401.      extern unsigned short hint;
  402.      extern short int TOflag;
  403.      extern short stage, stage2, Developed[];
  404.      extern short ChkFlag[], CptrFlag[], PawnThreat[];
  405.      extern short Pscore[], Tscore[];
  406.      extern short rehash;
  407.      extern unsigned int ttblsize;
  408.      extern short mtl[], pmtl[], hung[], emtl[];
  409.      extern short Pindex[];
  410.      extern short PieceCnt[];
  411.      extern short FROMsquare, TOsquare;
  412.      extern short HasKnight[], HasBishop[], HasRook[], HasQueen[];
  413.      extern const short qrook[];
  414.      extern const short krook[];
  415.      extern const short kingP[];
  416.      extern const short rank7[];
  417.      extern const short sweep[];
  418.      extern const short epmove1[], epmove2[];
  419.      extern unsigned short killr0[], killr1[];
  420.      extern unsigned short killr2[], killr3[];
  421.      extern unsigned short PV, SwagHt, Swag0, Swag1, Swag2, Swag3, Swag4, sidebit;
  422.      extern short __far killt[0x4000];
  423.      extern short mtl[2], pmtl[2], hung[2];
  424.      extern const short value[];
  425.      extern const short control[];
  426.      extern unsigned char __far nextpos[8][64][64];
  427.      extern unsigned char __far nextdir[8][64][64];
  428.      extern const short ptype[2][8];
  429.      extern long filesz;
  430.      extern FILE *hashfile;
  431.      extern unsigned int starttime;
  432.      extern short distdata[64][64], taxidata[64][64];
  433.      extern int bookcount;
  434.      extern unsigned long hashkey, hashbd;
  435.      extern struct hashval hashcode[2][7][64];
  436.      extern char *CP[];
  437. #ifdef QUIETBACKGROUND
  438.      extern short background;
  439. #endif /* QUIETBACKGROUND */
  440.  
  441. #ifdef ttblsz
  442.      extern struct hashentry huge ttable[2][vttblsz + MAXrehash];
  443.  
  444. #endif
  445.  
  446. /*
  447.  * hashbd contains a 32 bit "signature" of the board position. hashkey
  448.  * contains a 16 bit code used to address the hash table. When a move is
  449.  * made, XOR'ing the hashcode of moved piece on the from and to squares with
  450.  * the hashbd and hashkey values keeps things current.
  451.  */
  452. #define UpdateHashbd(side, piece, f, t) \
  453. {\
  454.   if ((f) >= 0)\
  455.     {\
  456.       hashbd ^= hashcode[side][piece][f].bd;\
  457.       hashkey ^= hashcode[side][piece][f].key;\
  458.     }\
  459.   if ((t) >= 0)\
  460.     {\
  461.       hashbd ^= hashcode[side][piece][t].bd;\
  462.       hashkey ^= hashcode[side][piece][t].key;\
  463.     }\
  464. }
  465.  
  466.  
  467.      extern short rpthash[2][256];
  468.      extern char *DRAW;
  469.  
  470. #define distance(a,b) distdata[a][b]
  471. #define row(a) ((a) >> 3)
  472. #define column(a) ((a) & 7)
  473. #define locn(a,b) (((a) << 3) | (b))
  474.      extern short distdata[64][64];
  475.  
  476. /* init external functions */
  477.      extern void NewGame (void);
  478.      extern int parse (FILE * fd, unsigned short *mv, short side, char *opening);
  479.      extern void GetOpenings (void);
  480.      extern int OpeningBook (unsigned short int *hint, short int side);
  481.      extern void SelectMove (short int side, short int iop);
  482.      extern short
  483.       search (short int side,
  484.            short int ply,
  485.            short int depth,
  486.            short int alpha,
  487.            short int beta,
  488.            unsigned short *bstline,
  489.            short int *rpt);
  490. #if ttblsz
  491.      extern int
  492.       ProbeTTable (short int side,
  493.             short int depth,
  494.             short int ply,
  495.             short int *alpha,
  496.             short int *beta,
  497.             short int *score);
  498.      extern int
  499.       PutInTTable (short int side,
  500.             short int score,
  501.             short int depth,
  502.             short int ply,
  503.             short int alpha,
  504.             short int beta,
  505.             unsigned short mv);
  506.      extern void ZeroTTable (void);
  507.      extern void ZeroRPT (void);
  508.      extern void Initialize_ttable (void);
  509. #ifdef HASHFILE
  510.      extern unsigned int urand (void);
  511.      extern void srand (unsigned int);
  512.      extern int
  513.       ProbeFTable (short int side,
  514.             short int depth,
  515.             short int ply,
  516.             short int *alpha,
  517.             short int *beta,
  518.             short int *score);
  519.      extern void
  520.       PutInFTable (short int side,
  521.             short int score,
  522.             short int depth,
  523.             short int ply,
  524.             short int alpha,
  525.             short int beta,
  526.             unsigned short int f,
  527.             unsigned short  int t);
  528.  
  529. #endif /* HASHFILE */
  530. #endif /* ttblsz */
  531.      extern void Initialize_moves (void);
  532.      extern void MoveList (short int side, short int ply);
  533.      extern void CaptureList (short int side, short int ply);
  534.      extern int castle (short int side, short int kf, short int kt, short int iop);
  535.      extern void
  536.       MakeMove (short int side,
  537.          struct leaf * node,
  538.          short int *tempb,
  539.          short int *tempc,
  540.          short int *tempsf,
  541.          short int *tempst,
  542.          short int *INCscore);
  543.      extern void
  544.       UnmakeMove (short int side,
  545.            struct leaf * node,
  546.            short int *tempb,
  547.            short int *tempc,
  548.            short int *tempsf,
  549.            short int *tempst);
  550.      extern void InitializeStats (void);
  551.      extern short
  552.       evaluate (short int side,
  553.          short int ply,
  554.          short int alpha,
  555.          short int beta,
  556.          short int INCscore,
  557.          short int *slk,
  558.          short int *InChk);
  559.      extern short int ScorePosition (short int side);
  560.      extern void ExaminePosition (void);
  561.      extern void UpdateWeights (void);
  562.      extern void Initialize (void);
  563.      extern void InputCommand (void);
  564.      extern void ExitChess (void);
  565.      extern void ClrScreen (void);
  566.      extern void SetTimeControl (void);
  567.      extern void SelectLevel (void);
  568.      extern void
  569.       UpdateDisplay (short int f,
  570.               short int t,
  571.               short int flag,
  572.               short int iscastle);
  573.      extern void ElapsedTime (short int iop);
  574.      extern void ShowSidetoMove (void);
  575.      extern void SearchStartStuff (short int side);
  576.      extern void ShowDepth (char ch);
  577.      extern void TerminateSearch (int);
  578.      extern void
  579.       ShowResults (short int score,
  580.             unsigned short  int *bstline,
  581.             char ch);
  582.      extern void PromptForMove (void);
  583.      extern void SetupBoard (void);
  584.      extern void algbr (short int f, short int t, short int flag);
  585.      extern void OutputMove (void);
  586.      extern void ShowCurrentMove (short int pnt, short int f, short int t);
  587.      extern void ListGame (void);
  588.      extern void ShowMessage (char *s);
  589.      extern void ClrScreen (void);
  590.      extern void gotoXY (short int x, short int y);
  591.      extern void ClrEoln (void);
  592.      extern void DrawPiece (short int sq);
  593.      extern void UpdateClocks (void);
  594.      extern void ShowLine (unsigned short  int *bstline);
  595.      extern void pick (short int p1, short int p2);
  596.      extern int TrComp (struct leaf * a, struct leaf * b);
  597.  
  598.  
  599. /* some more protos */
  600. #ifdef AMIGA
  601. void ataks (short int side, short int *a);
  602. unsigned int urand(void);
  603. void srand(unsigned int seed);
  604. #endif
  605.